// pages/dailydelivery/dailydelivery.js
var app = getApp();
var utils = require('../../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: ['商户', '果农'],
        isHide: true,
        type: [{
            key: 1,
            value: '果店加盟'
        }, {
            key: 2,
            value: '果农入驻'
        }, ],
        currentType: 1,

        uploadControls: [{
            tag: 'id_image',
            name: '营业执照',
        }, {
            tag: 'store_pic',
            name: '门头照',
        }, {
            tag: 'food_image',
            name: '食品经营许可证',
        }, {
            tag: 'quality_image',
            name: '质量证书',
        }, {
            tag: 'bank_image',
            name: '银行卡照片',
        }, ],
        images: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        utils.http("/api/v1/common/system", {}, function(res) {
            that.setData({
                systemData: res.datas
            })
        })
    },
    //进入详情
    dailyDetailsTap: function(e) {
        wx.navigateTo({
            url: '../dailydelivery/dailyDetails/dailyDetails',
        })
    },
    verify: function(param) {
        var that = this;
        // (/^1(3|4|5|7|8)\d{9}$/.test(param.username))
        var msg = '';
        if (!param.store_name) {
            msg = '姓名不能为空~';
        } else if (!param.username) {
            msg = '电话不能为空~';
        } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(param.username))) {
            msg = '电话格式不正确~';
        } else if (!param.map_address) {
            msg = '详细地址不能为空~';
        } else if (!param.bank_code) {
            msg = '银行卡不能为空~';
        } else if (!that.data.images['id_image']) {
            msg = '营业执照不能为空~';
        } else if (!that.data.images['quality_image']) {
            msg = '质量证书不能为空~';
        }
        console.log(msg)
        if (msg != '') {
            wx.showToast({
                title: msg,
                icon: 'none'
            });
            return false;
        }
        return true;
    },
    formSubmit: function(e) {
        var that = this;
        var param = e.detail.value;
        if (!that.verify(param)) return false;
        utils.http("/api/v1/member/add_store_v2", {
            store_name: param.store_name,
            username: param.username,
            map_address: param.map_address,
            inv_id: param.inv_id,
            bank_code: param.bank_code,
            type: that.data.currentType,
            id_image: that.data.images['id_image'] ? that.data.images['id_image'].id : 0,
            store_pic: that.data.images['store_pic'] ? that.data.images['store_pic'].id : 0,
            food_image: that.data.images['food_image'] ? that.data.images['food_image'].id : 0,
            quality_image: that.data.images['quality_image'] ? that.data.images['quality_image'].id : 0,
            bank_image: that.data.images['bank_image'] ? that.data.images['bank_image'].id : 0,
        }, function(res) {
            if (res.state == 1) {
                var money = that.data.currentType == 1 ? that.data.systemData.nong_yajin : that.data.systemData.dian_yajin;
                console.log('money', money);
                if (money <= 0) {
                    that.setData({
                        isHide: false
                    })
                    return null;
                }
                var response = res.datas;
                wx.requestPayment({
                    timeStamp: response.timeStamp,
                    nonceStr: response.nonceStr,
                    package: response.package,
                    signType: response.signType,
                    paySign: response.paySign,
                    success: function(e) {
                        console.log('支付', e)
                        that.setData({
                            isHide: false
                        })
                    }
                })
            } else {
                utils.message(res.datas)
            }
        })

    },
    chooseType: function(e) {
        var key = e.currentTarget.dataset.key;
        this.setData({
            currentType: key
        })
    },
    ticketUpload(e) {
        var tag = e.currentTarget.dataset.tag;
        console.log(tag)
        var that = this;
        that.uploadImg(function(data) {
            if (!data.id) {
                wx.showToast({
                    title: '上传失败',
                    icon: 'none'
                });
                return false;
            }
            var images = that.data.images;
            switch (tag) {
                case 'id_image':
                    images['id_image'] = {
                        id: data.id,
                        path: data.path
                    };
                    break;
                case 'store_pic':
                    images['store_pic'] = {
                        id: data.id,
                        path: data.path
                    };
                    break;
                case 'food_image':
                    images['food_image'] = {
                        id: data.id,
                        path: data.path
                    };
                    break;
                case 'quality_image':
                    images['quality_image'] = {
                        id: data.id,
                        path: data.path
                    };
                    break;
                case 'bank_image':
                    images['bank_image'] = {
                        id: data.id,
                        path: data.path
                    };
                    break;
            };
            that.setData({
                images: images
            });
        });
    },
    delImage: function(e) {
        var tag = e.currentTarget.dataset.tag;
        var that = this;
        delete that.data.images[tag];
        var images = that.data.images;
        that.setData({
            images: images
        })
    },
    //图片上传
    uploadImg: function(callback = '') {
        var that = this;
        // utils.showLoading();
        wx.chooseImage({
            count: 1,
            success(res) {
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.globalData.http + '/api/v1/common/upload',
                    header: {
                        'Content-Type': 'multipart/form-data'
                    },
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: app.globalData.key
                    },
                    success: function(result) {
                        utils.hideLoading();
                        var res = JSON.parse(result.data);
                        if (res.code != 1) {
                            wx.showToast({
                                title: '上传失败',
                                icon: 'none'
                            });
                            typeof callback == 'function' ? callback(false) : '';
                        } else {
                            typeof callback == 'function' ? callback(res) : '';
                        }
                    },
                    fail: function(res) {
                        utils.hideLoading();
                        utils.message('上传服务器上传失败,请稍后再试');
                        console.log(res);
                    }
                })
            }
        })
    },
    //选择加盟类型
    bindPickerChange(e) {
        var indexValue = ""
        if (e.detail.value == 0) {
            indexValue = 1
        }
        if (e.detail.value == 1) {
            indexValue = 2
        }
        this.setData({
            index: e.detail.value,
            indexValue: indexValue
        })
    },

    //关闭提交成功
    onCoseTap: function(e) {
        var that = this;
        that.setData({
            isHide: true
        }, function() {
            wx.navigateBack({
                delta: 1
            })
        })
    }
})